package com.gitee.wsl.time.chinese.bean

import com.gitee.wsl.time.chinese.Lunar
import com.gitee.wsl.time.chinese.LunarMonth
import com.gitee.wsl.time.chinese.util.FotoFestival
import com.gitee.wsl.time.chinese.util.FotoUtil
import com.gitee.wsl.time.chinese.util.FotoUtil.getXiu
import com.gitee.wsl.time.chinese.util.LunarUtil


/**
 * 佛历
 *
 * @author 6tail
 */
class Foto(
    /**
     * 阴历
     */
    val lunar: Lunar,
) {
    val year: Int
        get() {
            val sy = lunar.solar.year
            var y = sy - DEAD_YEAR
            if (sy == lunar.year) {
                y++
            }
            return y
        }

    val month: Int
        get() = lunar.month

    val day: Int
        get() = lunar.day

    val yearInChinese: String
        get() {
            val y = this.year.toString() + ""
            val s = StringBuilder()
            var i = 0
            val j = y.length
            while (i < j) {
                s.append(LunarUtil.NUMBER[y.get(i).code - '0'.code])
                i++
            }
            return s.toString()
        }

    val monthInChinese: String
        get() = lunar.monthInChinese

    val dayInChinese: String
        get() = lunar.dayInChinese

    val festivals: List<FotoFestival?>
        /**
         * 获取因果犯忌
         * @return 因果犯忌
         */
        get() {
            val l= ArrayList<FotoFestival>()
            val fs = FotoUtil.FESTIVAL.get(this.month.toString() + "-" + this.day)
            if (null != fs) {
                l.addAll(fs)
            }
            return l
        }

    val otherFestivals: List<String>
        /**
         * 获取纪念日
         * @return 纪念日
         */
        get() {
            val l = ArrayList<String>()
            val fs = FotoUtil.OTHER_FESTIVAL.get(this.month.toString() + "-" + this.day)
            if (null != fs) {
                l.addAll(fs)
            }
            return l
        }

    val isMonthZhai: Boolean
        /**
         * 是否月斋
         *
         * @return true/false
         */
        get() {
            val m = this.month
            return 1 == m || 5 == m || 9 == m
        }

    val isDayYangGong: Boolean
        /**
         * 是否杨公忌
         *
         * @return true/false
         */
        get() {
            for (f in this.festivals) {
                if ("杨公忌" == f!!.name) {
                    return true
                }
            }
            return false
        }

    val isDayZhaiShuoWang: Boolean
        /**
         * 是否朔望斋
         *
         * @return true/false
         */
        get() {
            val d = this.day
            return 1 == d || 15 == d
        }

    val isDayZhaiSix: Boolean
        /**
         * 是否六斋日
         *
         * @return true/false
         */
        get() {
            val d = this.day
            if (8 == d || 14 == d || 15 == d || 23 == d || 29 == d || 30 == d) {
                return true
            } else if (28 == d) {
                val m = LunarMonth.Companion.fromYm(lunar.year, this.month)
                return null != m && 30 != m.dayCount
            }
            return false
        }

    val isDayZhaiTen: Boolean
        /**
         * 是否十斋日
         *
         * @return true/false
         */
        get() {
            val d = this.day
            return 1 == d || 8 == d || 14 == d || 15 == d || 18 == d || 23 == d || 24 == d || 28 == d || 29 == d || 30 == d
        }

    val isDayZhaiGuanYin: Boolean
        /**
         * 是否观音斋
         *
         * @return true/false
         */
        get() {
            val k = this.month.toString() + "-" + this.day
            for (d in FotoUtil.DAY_ZHAI_GUAN_YIN) {
                if (k == d) {
                    return true
                }
            }
            return false
        }

    val xiu: String
        /**
         * 获取星宿
         *
         * @return 星宿
         */
        get() = getXiu(this.month, this.day)

    val xiuLuck: String
        /**
         * 获取宿吉凶
         *
         * @return 吉/凶
         */
        get() = LunarUtil.XIU_LUCK.get(this.xiu)!!

    val xiuSong: String
        /**
         * 获取宿歌诀
         *
         * @return 宿歌诀
         */
        get() = LunarUtil.XIU_SONG.get(this.xiu)!!

    val zheng: String
        /**
         * 获取政
         *
         * @return 政
         */
        get() = LunarUtil.ZHENG.get(this.xiu)!!

    val animal: String
        /**
         * 获取动物
         *
         * @return 动物
         */
        get() = LunarUtil.ANIMAL.get(this.xiu)!!

    val gong: String
        /**
         * 获取宫
         *
         * @return 宫
         */
        get() = LunarUtil.GONG.get(this.xiu)!!

    val shou: String
        /**
         * 获取兽
         *
         * @return 兽
         */
        get() = LunarUtil.SHOU.get(this.gong)!!

    override fun toString(): String {
        return this.yearInChinese + "年" + this.monthInChinese + "月" + this.dayInChinese
    }

    fun toFullString(): String {
        val s = StringBuilder()
        s.append(this)
        for (f in this.festivals) {
            s.append(" (")
            s.append(f)
            s.append(")")
        }
        return s.toString()
    }

    companion object {
        val DEAD_YEAR: Int = -543

        fun fromLunar(lunar: Lunar): Foto {
            return Foto(lunar)
        }

        fun fromYmdHms(year: Int, month: Int, day: Int, hour: Int, minute: Int, second: Int): Foto {
            return fromLunar(Lunar.Companion.fromYmdHms(year + DEAD_YEAR - 1, month, day, hour, minute, second))
        }

        fun fromYmd(year: Int, month: Int, day: Int): Foto {
            return fromYmdHms(year, month, day, 0, 0, 0)
        }
    }
}
